package com.purchase.common.api;


import cn.com.flaginfo.sdk.cmc.api.ApiProvider;
import cn.com.flaginfo.sdk.cmc.api.request.ApiConfig;
import cn.com.flaginfo.sdk.cmc.api.result.ComResult;
import cn.com.flaginfo.sdk.cmc.api.sms.dynsend.DynSMSAPI;
import cn.com.flaginfo.sdk.cmc.api.sms.dynsend.DynSMSSendDataResult;
import cn.com.flaginfo.sdk.cmc.api.sms.dynsend.DynSMSSendRequest;
import cn.com.flaginfo.sdk.cmc.api.sms.reply.SMSReplyAPI;
import cn.com.flaginfo.sdk.cmc.api.sms.reply.SMSReplyDataResult;
import cn.com.flaginfo.sdk.cmc.api.sms.reply.SMSReplyRequest;
import cn.com.flaginfo.sdk.cmc.api.sms.replyconfirm.SMSReplyConfirmAPI;
import cn.com.flaginfo.sdk.cmc.api.sms.replyconfirm.SMSReplyConfirmRequest;
import cn.com.flaginfo.sdk.cmc.api.sms.report.SMSReportAPI;
import cn.com.flaginfo.sdk.cmc.api.sms.report.SMSReportRequest;
import cn.com.flaginfo.sdk.cmc.api.sms.report.SMSReportResult;
import cn.com.flaginfo.sdk.cmc.api.sms.send.SMSApi;
import cn.com.flaginfo.sdk.cmc.api.sms.send.SMSSendDataResult;
import cn.com.flaginfo.sdk.cmc.api.sms.send.SMSSendRequest;
import cn.com.flaginfo.sdk.cmc.common.ApiEnum;
import cn.com.flaginfo.sdk.cmc.util.JSONUtil;
import com.purchase.common.api.ext.ExtSDKDemoApi;
import com.purchase.common.api.ext.model.ExtApiRequest;
import com.purchase.common.api.ext.model.ExtApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;


public class SDKApi {

	Logger log = LoggerFactory.getLogger(SDKApi.class);
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
	Random rd = new Random();

	ApiConfig apiConfig = null;
	ApiProvider provider = null;

	public SDKApi(ApiConfig apiConfig) {
		this.apiConfig = apiConfig;
		provider = ApiProvider.getInstance(apiConfig);
		//注册拓展的Api
		provider.regExtApi("testExtApi_1",new ExtSDKDemoApi(apiConfig));

	}

	public ComResult<SMSSendDataResult> testSend(SMSSendRequest sendRequest){
		SMSApi api = provider.getApi(ApiEnum.SENDSMS);
		ComResult<SMSSendDataResult> response = api.request(sendRequest);
		log.info(JSONUtil.parseToJson(response));
		return response;
	}


	/**
	 * 动态短信内容批量发送
	 */
	public ComResult<DynSMSSendDataResult> testDynSendMethod(DynSMSSendRequest sendRequest) {
		DynSMSAPI api = provider.getApi(ApiEnum.SENDDYNSMS);
		ComResult<DynSMSSendDataResult> result = api.request(sendRequest);
		log.info(JSONUtil.parseToJson(result));
		return result;
	}

	/**
	 * 状态报告
	 */
	public ComResult<List<SMSReportResult>> testReportMethod() {
		//请求接口
		SMSReportAPI api = provider.getApi(ApiEnum.REPORTSMS);
		//请求
		ComResult<List<SMSReportResult>> result = api.request(new SMSReportRequest());
		log.info(JSONUtil.parseToJson(result));
		return result;
	}

	/**
	 * 上行回复查询
	 */
	public ComResult<SMSReplyDataResult> testReplyMethod() {
		//请求接口
		SMSReplyAPI api = provider.getApi(ApiEnum.REPLYSMS);
		SMSReplyRequest smsReplyRequest = new SMSReplyRequest();
		//请求
		ComResult<SMSReplyDataResult> result = api.request(smsReplyRequest);
		log.info(JSONUtil.parseToJson(result));
		return result;
	}

	/**
	 * 上行回复查询数据收到确认,有上行数据才调用确认(如果没有数据可以不用确认)
	 */
	public ComResult<String> testReplyConfirmMethod(String maxId) {
		//请求接口
		SMSReplyConfirmAPI api = provider.getApi(ApiEnum.REPLYSMSCONFIRM);
		//请求Url
		SMSReplyConfirmRequest req = new SMSReplyConfirmRequest();
		req.setMaxId(maxId);
		ComResult<String> result = api.request(req);
		log.info(JSONUtil.parseToJson(result));
		return result;
	}

	/**
	 * 拓展API
	 * @return
	 */
	public ComResult<ExtApiResult> testExtApi(){

		ExtSDKDemoApi api = provider.getExtApi("testExtApi_1");
		//以下测试随机参数,请替换成自己真实发送数据
		Random rd = new Random();
		String name = "张" + rd.nextInt(1000);
		String month = String.valueOf(rd.nextInt(12));
		String money = rd.nextInt(10000) + ".5";
		String content = "尊敬的用户" + name + "您" + month + "账单已经出来啦!总共消费:" + money + "元人民币";
		//请求参数
		ExtApiRequest sendRequest = new ExtApiRequest();
		sendRequest.setMessageContent(content);
		sendRequest.setTemplateId("101****14702");
		sendRequest.setUserNumber("135****4647");
		int seq = rd.nextInt(900000) + 100000;
		sendRequest.setSerialNumber(sdf.format(new Date()) + seq);//流水号,状态回执报告中一致
		ComResult<ExtApiResult> response = api.request(sendRequest);
		log.info(JSONUtil.parseToJson(response));
		return response;
	}

}
